﻿using UnityEngine;
using System.Collections;


namespace ShooterGameMOD_Besiege.Player
{

    /// <summary>
    /// 玩家控制脚本
    /// </summary>
    public class PlayerControllerScript : MonoBehaviour
    {

        CharacterController controller;

        public float speed = 0.1F;

        //移动速度
        public float speed_move = 12f;
        //奔跑速度
        public float speed_run = 30f;
        //跳跃速度
        public float speed_jump = 12F;
        //重力系数
        public float gravity = 20.0F;
        //镜头转动速度
        public float speed_rotation = 2f;
        private Vector3 moveDirection = Vector3.zero;

        //主摄像头
        public GameObject MainCamera;
        //武器
        //public GameObject Weapon;
        //玩家镜头状态
        public PlayerCameraState playerCameraState = PlayerCameraState.Observer;

        //玩家镜头状态枚举：观察，第一人称，第三人称
        public enum PlayerCameraState
        {
            /// <summary>
            /// 观察者
            /// </summary>
            Observer = 0,
            /// <summary>
            /// 第一人称
            /// </summary>
            First = 1,
            /// <summary>
            /// 第三人称
            /// </summary>
            Third = 2
        }

        public MKey key_Fire;
        public MKey key_ChangeCamera;
        public MKey key_DropWeapon;
        public MKey key_Run;
        public MKey key_Reload;

        //开火
        public KeyCode Fire = KeyCode.Mouse0;
        //丢武器
        public KeyCode dropWeapon = KeyCode.K;
        //改变视角
        public KeyCode changeCamera = KeyCode.V;
        //加速
        public KeyCode run = KeyCode.X;
        //换弹
        public KeyCode reload = KeyCode.U;

        // Use this for initialization
        void Start()
        {
            controller = GetComponent<CharacterController>();
            key_ChangeCamera = new MKey("Camera State", "ChangeCamera", changeCamera);
            key_DropWeapon = new MKey("Drop Weapon", "DropWeapon", dropWeapon);
            key_Run = new MKey("Run", "Run", run);
            key_Fire = new MKey("Fire", "Fire", Fire);
        }

        // Update is called once per frame
        void Update()
        {

            if (StatMaster.isSimulating)
            {
                if (controller.isGrounded)
                {
                    //moveDirection = new Vector3(Input.GetAxis("Mouse Y")*10f, 0, Input.GetAxis("Mouse X")*10f);
                    //Debug.Log(string.Format("{0} + {1}", Input.GetAxis("Mouse Y"), Input.GetAxis("Mouse X")));
                    moveDirection = Vector3.zero;
                    moveDirection = transform.TransformDirection(moveDirection);
                    moveDirection *= speed;
                    if (Input.GetKey(KeyCode.J))
                        moveDirection.y = speed_jump;

                }

                moveDirection.y -= gravity * Time.deltaTime;
                controller.Move(moveDirection * Time.deltaTime);

                if (MainCamera == null)
                {
                    MainCamera = GameObject.Find("Main Camera");
                }
                else
                {
                    if (key_ChangeCamera.IsPressed)
                    {
                        switch (playerCameraState)
                        {
                            case PlayerCameraState.Observer:
                                playerCameraState = PlayerCameraState.Third;
                                transform.Rotate(Vector3.up * (MainCamera.GetComponent<MouseOrbit>().x - transform.eulerAngles.y), Space.Self);
                                break;
                            case PlayerCameraState.Third:
                                playerCameraState = PlayerCameraState.First;
                                transform.Rotate(Vector3.up * (MainCamera.GetComponent<MouseOrbit>().x - transform.eulerAngles.y), Space.Self);
                                MainCamera.GetComponent<MouseOrbit>().wasdPosOffset = new Vector3(0, 3, 0);
                                break;
                            default: playerCameraState = PlayerCameraState.Observer; break;
                        }
                    }
                }

                if (playerCameraState != PlayerCameraState.Observer)
                {
                    MouseOrbit mo = MainCamera.GetComponent<MouseOrbit>();
                    MainCamera.GetComponent<MouseOrbit>().x += Input.GetAxis("Mouse X") * speed_rotation;
                    MainCamera.GetComponent<MouseOrbit>().y -= Input.GetAxis("Mouse Y") * speed_rotation;
                    MainCamera.GetComponent<MouseOrbit>().target = transform;

                    //transform.eulerAngles = new Vector3(0,MainCamera.GetComponent<MouseOrbit>().x,0);
                    transform.Rotate(Vector3.up * speed_rotation * Input.GetAxis("Mouse X"), Space.Self);

                    if (playerCameraState == PlayerCameraState.Third)
                    {
                        mo.distance = 50f;
                    }

                    if (playerCameraState == PlayerCameraState.First)
                    {
                        mo.distance = 0f;
                    }

                    if (Cursor.lockState != CursorLockMode.Confined)
                    {
                        Debug.Log("锁定鼠标");
                        Cursor.lockState = CursorLockMode.Confined;
                        Cursor.visible = false;
                    }

                }
                else
                {
                    Cursor.lockState = CursorLockMode.None;
                    Cursor.visible = true;
                }

                if (key_Run.IsDown)
                {
                    speed_move = Mathf.MoveTowards(speed_move, 24f, 2f);

                }
                else
                {
                    speed_move = Mathf.MoveTowards(speed_move, 12f, 2f);
                }

                if (Input.GetKey(KeyCode.F) | Input.GetKey(KeyCode.LeftArrow)) //左
                {
                    transform.Translate(Vector3.right * -speed_move * Time.deltaTime);
                }
                if (Input.GetKey(KeyCode.H) | Input.GetKey(KeyCode.RightArrow)) //右
                {
                    transform.Translate(Vector3.right * speed_move * Time.deltaTime);
                }
                if (Input.GetKey(KeyCode.T) | Input.GetKey(KeyCode.UpArrow)) //前
                {
                    transform.Translate(Vector3.forward * speed_move * Time.deltaTime);
                }
                if (Input.GetKey(KeyCode.G) | Input.GetKey(KeyCode.DownArrow)) //后
                {
                    transform.Translate(Vector3.forward * -speed_move * Time.deltaTime);
                }
            }
            else
            {
                //Destroy(Weapon);
            }

        }
    }
}
